package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbProjectEngineeringDo;
import com.yxx.business.entity.TbProjectEngineeringVo;
import com.yxx.business.entity.TbProjectEngineeringQueryDto;
import com.yxx.business.entity.TbProjectEngineeringEditDto;
import com.yxx.business.service.ITbProjectEngineeringService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 项目-工程关联Controller
 *
 * @author yxx
 * @date 2025-08-03
 */
@Tag(name = "项目-工程关联管理-TbProjectEngineering")
@RestController
@RequestMapping("/business/project/engineering")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbProjectEngineeringController extends BaseControllerPlus {

    private final ITbProjectEngineeringService tbProjectEngineeringService;

    @Operation(summary = "查询--项目-工程关联列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:list')")
    @GetMapping("/list")
    public PageResult<TbProjectEngineeringVo> list(@ParameterObject TbProjectEngineeringQueryDto tbProjectEngineeringQueryDto) {
        return tbProjectEngineeringService.selectTbProjectEngineeringVoPage(tbProjectEngineeringQueryDto);
    }

    @Operation(summary = "查询--项目-工程关联单个")
    @PreAuthorize("@ss.hasPermi('business:engineering:query')")
    @GetMapping(value = "/{id}")
    public R<TbProjectEngineeringVo> getInfo(@PathVariable Long id) {
        return R.ok(tbProjectEngineeringService.selectTbProjectEngineeringVoById(id));
    }

    @Operation(summary = "新增--项目-工程关联")
    @PreAuthorize("@ss.hasPermi('business:engineering:add')")
    @Log(title = "项目-工程关联", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbProjectEngineeringEditDto tbProjectEngineeringQueryDto) {
        return toResult(tbProjectEngineeringService.insertTbProjectEngineering(tbProjectEngineeringQueryDto));
    }

    @Operation(summary = "修改--项目-工程关联")
    @PreAuthorize("@ss.hasPermi('business:engineering:edit')")
    @Log(title = "项目-工程关联", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public R<String> update(@PathVariable Long id, @RequestBody @Validated TbProjectEngineeringEditDto tbProjectEngineeringQueryDto) {
        return toResult(tbProjectEngineeringService.updateTbProjectEngineering(id, tbProjectEngineeringQueryDto));
    }

    @Operation(summary = "删除--项目-工程关联")
    @PreAuthorize("@ss.hasPermi('business:engineering:remove')")
    @Log(title = "项目-工程关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<String> delete(@PathVariable List<Long> ids) {
        return toResult(tbProjectEngineeringService.deleteTbProjectEngineeringByIds(ids));
    }

    @Operation(summary = "导出--项目-工程关联列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:export')")
    @Log(title = "项目-工程关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbProjectEngineeringQueryDto tbProjectEngineeringQueryDto) {
        List<TbProjectEngineeringDo> list = tbProjectEngineeringService.selectTbProjectEngineeringDoList(tbProjectEngineeringQueryDto);
        ExcelUtil<TbProjectEngineeringDo> util = new ExcelUtil<>(TbProjectEngineeringDo.class);
        util.exportExcel(response, list, "项目-工程关联数据");
    }

    @Operation(summary = "导出--项目-工程关联模板")
    @PreAuthorize("@ss.hasPermi('business:engineering:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbProjectEngineeringDo> util = new ExcelUtil<>(TbProjectEngineeringDo.class);
        util.importTemplateExcel(response, "导入项目-工程关联模板数据");
    }

    @Operation(summary = "导入--项目-工程关联列表")
    @PreAuthorize("@ss.hasPermi('business:engineering:import')")
    @Log(title = "项目-工程关联", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbProjectEngineeringDo> util = new ExcelUtil<>(TbProjectEngineeringDo.class);
        List<TbProjectEngineeringDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbProjectEngineeringService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
